package com.mhkj.revise.plan.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.busiMode.service.BusiModelService;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.calender.service.WorkCalenderService;
import com.mhkj.base.functionOffice.entity.FunctionOffice;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.common.enums.PlanType;
import com.mhkj.common.util.ItemHelper;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.repository.PlanNodeRepository;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mhkj.revise.plan.service.PlanService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Company;
import com.mics.core.system.organizational.bean.Office;
import com.mics.core.system.organizational.bean.Post;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.organizational.service.PermissionService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 计划节点名称
 * @author mics
 * @date 2018年7月6日
 * @version 1.0
 */
@Service
public class PlanNodeServiceImpl extends BaseServiceImpl<PlanNode> implements PlanNodeService{
	@Resource
	private PlanNodeRepository planNodeRepository;
	@Resource
	private WorkCalenderService calenderService;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private OrgService orgService;
	@Resource
	private PermissionService permissionService;
	@Resource
	private BusiModelService busiModelService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private ItemService itemService;
	@Resource
	private PlanService planService;
	@Resource
	private ItemHelper itemHelper;
	
	@Resource
	public void setPlanNodeRepository(PlanNodeRepository planNodeRepository) {
		super.setBaseRepository(planNodeRepository);
	}
	

	@Override
	public List<PlanNode> findKeyNodebyEmpId(String empId, String year, String taskType){
		Specification<PlanNode> spec = new Specification<PlanNode>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = 8020404975688176639L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				if (!StringUtils.isEmpty(year)) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					try {
						Date date  = sdf.parse(year+"-01-01");
						DateTime dateTime = new DateTime(date.getTime());
						//获取当前年份的最后一天然后加1
						Date maxday = dateTime.dayOfYear().withMinimumValue().toDate();
						//获取当前年份的第一天然后减1
						Date miniday = dateTime.dayOfYear().withMaximumValue().toDate();
						//先取正常结束
						Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class), maxday,miniday);
						//推迟时间在年份内
						Predicate lastbetween = criteriaBuilder.between(root.get("lateDate").as(Date.class), maxday,miniday);
						criteriaBuilder.or(between,lastbetween);
						
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				
				if (!StringUtils.isEmpty(empId)) {
					lstPredicates.add(criteriaBuilder.equal(root.get("managerId").as(String.class),empId));
				}
				
				if (!StringUtils.isEmpty(taskType)) {
					lstPredicates.add(criteriaBuilder.equal(root.get("taskType").as(String.class), taskType));
				}
				
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
	 
		return findAll;
	}

	
	/**
	 * 根据编号查询
	 * @author mics
	 * @date 2018年7月9日
	 * @version 1.0
	 */
	public PlanNode getPlanNodeByNO(String NO){
		return planNodeRepository.getPlanNodeByNO(NO);
	}
	

	@Override
	public Pagination<PlanNode> listByPage(PlanNode planNode, int pageIndex,int pageSize) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              if(!StringUtils.isEmpty(planNode.getNodeName())){
                  Predicate p1=criteriaBuilder.like(root.get("name").as(String.class), "%"+planNode.getNodeName()+"%");
                  lstPredicates.add(p1);
              }
              if(planNode.getPlan() != null){
                  Join<Object, Object> planType = root.join("plan",JoinType.INNER);
                  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planNode.getPlan().getId());
                  lstPredicates.add(p1);
              }
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<PlanNode> pageT =  planNodeRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<PlanNode> pagination = new Pagination<PlanNode>();
		pagination.setTotal(planNodeRepository.count(spec));
		List<PlanNode> plans = pageT.getContent();
		for(PlanNode planNode2 : plans){
			Company company = orgService.getCompanyById(planNode2.getCompanyId());
			planNode2.setCompanyName(company.getCompanyname());
			Office cOffice = orgService.officeInfo(planNode2.getOfficeId());
			planNode2.setOfficeName(cOffice.getName());
			Post post = orgService.getRoleInfo(planNode2.getPostId());
			planNode2.setPostName(post.getName());
		}
		pagination.setData(plans);
		return pagination;
	}



	@Override
	public List<PlanNode> queryBusiSysPlan() {
		List<PlanNode> planNodes = planNodeRepository.queryBusiSysPlan();
		for(PlanNode planNode2 : planNodes){
			Office cOffice = orgService.officeInfo(planNode2.getOfficeId());
			planNode2.setOfficeName(cOffice.getName());
		}
		return planNodes;
	}
	/**
	 * 我关注的计划全部
	 */
	@Override
	public Pagination<PlanNode> findCarePlanPage(List<Long> listl, Long itemId, Integer state, Date month, int pageIndex, int pageSize) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1388146714896462789L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              
              

				if (listl != null && listl.size() > 0) {
					In<Object> in = criteriaBuilder.in(root.get("id"));
					for (Long id : listl) {
						in.value(id);
					}
					lstPredicates.add(in);
				}
           
              
              if(!StringUtils.isEmpty(itemId)){
            	  
            	  Join<Object, Object> plan = root.join("plan", JoinType.LEFT);
            	  Join<Object, Object> item = plan.join("item", JoinType.LEFT);
                 
            	  Predicate p1=criteriaBuilder.equal(item.get("name").as(Long.class),itemId);
                  lstPredicates.add(p1);
              }

              if(!StringUtils.isEmpty(state)){
                  Predicate p1=criteriaBuilder.equal(root.get("state").as(Integer.class),state);
                  lstPredicates.add(p1);
              }
              
              if(!StringUtils.isEmpty(month)) {
            	  DateTime dateTime = new DateTime(month.getTime());
					//获取当前月份最小值减去一天就是 上月最后一天,
					Date miniday = dateTime.dayOfMonth().withMinimumValue().toDate();
					//获取当前月份的最后一天加上一天就是 下月的第一天
					Date maxday = dateTime.dayOfMonth().withMaximumValue().toDate();
				
					// 计划的结束时间大于当月的上月最后一天
					Predicate greaterThan = criteriaBuilder.greaterThan(root.get("endDate").as(Date.class), miniday);
					
					//计划的开始时间小于当前查询月份的下月第一天
					Predicate lessThan = criteriaBuilder.lessThan(root.get("beginDate").as(Date.class), maxday);
					
					lstPredicates.add(greaterThan);
					lstPredicates.add(lessThan);
              }
              
              
              
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<PlanNode> pageT =  planNodeRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<PlanNode> pagination = new Pagination<PlanNode>();
		pagination.setTotal(planNodeRepository.count(spec));
		List<PlanNode> plans = pageT.getContent();
		for(PlanNode planNode2 : plans){
			Office cOffice = orgService.officeInfo(planNode2.getOfficeId());
			planNode2.setOfficeName(cOffice.getName());
			
		}
		pagination.setData(plans);
		return pagination;
	}

	@Override
	public Pagination<PlanNode> listMySubordinatePage(List<String> list, Long itemId, Integer state, Date month,
			int pageIndex, int pageSize) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1388146714896462789L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
				if (list != null && list.size() > 0) {
					In<Object> in = criteriaBuilder.in(root.get("managerId"));
					for (String id : list) {
						in.value(id);
					}
					lstPredicates.add(in);
				}
             
              if(!StringUtils.isEmpty(itemId)){
            	  
            	  Join<Object, Object> plan = root.join("plan", JoinType.LEFT);
            	  Join<Object, Object> item = plan.join("item", JoinType.LEFT);
                 
            	  Predicate p1=criteriaBuilder.equal(item.get("id"),itemId);
                  lstPredicates.add(p1);
              }

              if(!StringUtils.isEmpty(state)){
                  Predicate p1=criteriaBuilder.equal(root.get("state").as(Integer.class),state);
                  lstPredicates.add(p1);
              }
              
              if(!StringUtils.isEmpty(month)) {
            	  DateTime dateTime = new DateTime(month.getTime());
					//获取当前月份最小值减去一天就是 上月最后一天,
					Date miniday = dateTime.dayOfMonth().withMinimumValue().toDate();
					//获取当前月份的最后一天加上一天就是 下月的第一天
					Date maxday = dateTime.dayOfMonth().withMaximumValue().toDate();
				
					// 计划的结束时间大于当月的上月最后一天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), miniday);
					
					//计划的开始时间小于当前查询月份的下月第一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class), maxday);
					
					lstPredicates.add(greaterThan);
					lstPredicates.add(lessThan);
              }
              
              
              
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<PlanNode> pageT =  planNodeRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<PlanNode> pagination = new Pagination<PlanNode>();
		pagination.setTotal(planNodeRepository.count(spec));
		List<PlanNode> plans = pageT.getContent();
		for(PlanNode planNode2 : plans){
			Office cOffice = orgService.officeInfo(planNode2.getOfficeId());
			planNode2.setOfficeName(cOffice.getName());
		}
		pagination.setData(plans);
		return pagination;
	}

	/**
	 * 根据编号查询
	 * @author mics
	 * @date 2018年7月9日
	 * @version 1.0
	 */
	public List<Object> countList(){
		return planNodeRepository.countList();
	}

	@Override
	public List<PlanNode> findChildList(Long id) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1388146714896462789L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              
              if(!StringUtils.isEmpty(id)){
                  Predicate p1=criteriaBuilder.equal(root.get("planNode"),id);
                  lstPredicates.add(p1);
              }

              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		 List<PlanNode> findAll = planNodeRepository.findAll(spec);
		 
		return findAll;
	}

	
	

	/**
	 * 获取与当前条件相符的所有节点
	 */
	@Override
	public List<PlanNode> getAllTask(Long planId, Long month, Long year,Long planTypeId,String code, Long functionOffice) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
             
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
              if(!StringUtils.isEmpty(planId)){
            	  lstPredicates.add(criteriaBuilder.equal(plan.get("id").as(Long.class),planId));
              }else {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  if(!StringUtils.isEmpty(planTypeId)){
            		  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
                	  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                	  lstPredicates.add(p1);
                  }
            	  }
              }
             //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
              
              if(!PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
            	  Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
            	  if(itemHelper.getCurrentUserItems().size() > 0 ){
    					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
    		            lstPredicates.add(p3);
                    }else{
    					Predicate p3=criteriaBuilder.and(item.isNull());
    		            lstPredicates.add(p3);
                    }
            	  }
              }else {
            	  if(!StringUtils.isEmpty(functionOffice)){
            		  Join<Object, Object> fo = plan.join("functionOffice",JoinType.INNER);
            	  Predicate p1=criteriaBuilder.equal(fo.get("id").as(Long.class), functionOffice);
            	  lstPredicates.add(p1);
            	  }
              }
              
              if(!StringUtils.isEmpty(month)){
            		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThan(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
              }else {
            	  if(!StringUtils.isEmpty(year)){
            		  DateTime dateTime = new DateTime(year);
  					// 获取传入月份第一天然后减1就是上月最后一天
  					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
  					// 结束时间大于上月最后一天
  					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThan(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
            	  }
              }
              
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
		
		return findAll;
	}


	@Override
	public List<PlanNode> getShouldCompletedTask(Long planId, Long month, Long year,Long planTypeId,String code, Long functionOffice) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			 
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 924741607349941021L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
             
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
              if(!StringUtils.isEmpty(planId)){
            	  lstPredicates.add(criteriaBuilder.equal(plan.get("id").as(Long.class),planId));
              }else {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  if(!StringUtils.isEmpty(planTypeId)){
            		  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
                	  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                	  lstPredicates.add(p1);
                  }
            	  }
              }
              
              //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
             if( !PlanType.FUNC_TYPE.getCode().equals(code)) {
            	 if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
              Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
              if(itemHelper.getCurrentUserItems().size() > 0 ){
					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
		            lstPredicates.add(p3);
                }else{
					Predicate p3=criteriaBuilder.and(item.isNull());
		            lstPredicates.add(p3);
                }
            	 }
             }else {
           	  if(!StringUtils.isEmpty(functionOffice)){
        		  Join<Object, Object> fo = plan.join("functionOffice",JoinType.INNER);
        	  Predicate p1=criteriaBuilder.equal(fo.get("id").as(Long.class), functionOffice);
        	  lstPredicates.add(p1);
        	  }
          }
              if(!StringUtils.isEmpty(month)){
            		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
              }else {
            	  if(!StringUtils.isEmpty(year)){
            		DateTime dateTime = new DateTime(year);
  					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
  					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
  					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
            	  }
              }
              
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
		
		return findAll;
	}


	@Override
	public List<PlanNode> getCompletedOnTimeTask(Long planId, Long month, Long year,Long planTypeId,String code, Long functionOffice) {
		
		
		
		
		
		
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			 
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 924741607349941021L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
             
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
              if(!StringUtils.isEmpty(planId)){
            	  lstPredicates.add(criteriaBuilder.equal(plan.get("id").as(Long.class),planId));
              }else {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  if(!StringUtils.isEmpty(planTypeId)){
            		  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
                	  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                	  lstPredicates.add(p1);
                  }
            	  } 
              }
              
              
              //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
              if( !PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
              Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
              if(itemHelper.getCurrentUserItems().size() > 0 ){
					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
		            lstPredicates.add(p3);
                }else{
					Predicate p3=criteriaBuilder.and(item.isNull());
		            lstPredicates.add(p3);
                }
            	  }
              }else {
            	  if(!StringUtils.isEmpty(functionOffice)){
            		  Join<Object, Object> fo = plan.join("functionOffice",JoinType.INNER);
            	  Predicate p1=criteriaBuilder.equal(fo.get("id").as(Long.class), functionOffice);
            	  lstPredicates.add(p1);
            	  }
              }
              if(!StringUtils.isEmpty(month)){
            		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					//Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					//Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
              }else {
            	  if(!StringUtils.isEmpty(year)){
            		DateTime dateTime = new DateTime(year);
  					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
  					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
  					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
            	  }
              }
              
              //小于等于
              Predicate lessThanOrEqualTo = criteriaBuilder.lessThanOrEqualTo(root.get("completeDate").as(Date.class), root.get("endDate").as(Date.class));
              Predicate lessThanOrEqualTo2 = criteriaBuilder.lessThanOrEqualTo(root.get("completeDate").as(Date.class), root.get("delayDate").as(Date.class));
              
              lstPredicates.add(criteriaBuilder.or(lessThanOrEqualTo, lessThanOrEqualTo2));
              
              
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
		
		 
		 
		return findAll;
	}

//逾期
	@Override
	public List<PlanNode> getOverdue(Long planId, Long month, Long year,Long planTypeId,String code, Long functionOffice) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			 
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 924741607349941021L;

			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
             
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
              
              if(!StringUtils.isEmpty(planId)){
            	  lstPredicates.add(criteriaBuilder.equal(plan.get("id").as(Long.class),planId));
              }else {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  if(!StringUtils.isEmpty(planTypeId)){
            		  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
                	  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                	  lstPredicates.add(p1);
                  }
            	  }
              }
              
              //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
              if( !PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
		              Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
		              if(itemHelper.getCurrentUserItems().size() > 0 ){
		  					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
		  		            lstPredicates.add(p3);
		                  }else{
		  					Predicate p3=criteriaBuilder.and(item.isNull());
		  		            lstPredicates.add(p3);
		                  }
		            	  }
              }else {
            	  if(!StringUtils.isEmpty(functionOffice)){
            		  Join<Object, Object> fo = plan.join("functionOffice",JoinType.INNER);
            	      Predicate p1=criteriaBuilder.equal(fo.get("id").as(Long.class), functionOffice);
            	     lstPredicates.add(p1);
            	  }
              }
              if(!StringUtils.isEmpty(month)){
            		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					//Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
				//	Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
              }else {
            	  if(!StringUtils.isEmpty(year)){
            		DateTime dateTime = new DateTime(year);
  					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
  					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
  					Predicate between = criteriaBuilder.between(root.get("endDate").as(Date.class),mini,max);
					Predicate between1 = criteriaBuilder.between(root.get("delayDate").as(Date.class),mini,max);
					lstPredicates.add(criteriaBuilder.or(between, between1));
            	  }
              }
              lstPredicates.add(criteriaBuilder.gt(root.get("overDay").as(Integer.class), 0));
 //             lstPredicates.add(criteriaBuilder.isNotNull(root.get("completeDate").as(Integer.class)));
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
		
		return findAll;
	}


	@Override
	public List<PlanNode> getCompletedTask(Long planId, Long month, Long year,Long planTypeId,String code, Long functionOffice) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
             
              if(!StringUtils.isEmpty(planId)){
            	  lstPredicates.add(criteriaBuilder.equal(plan.get("id").as(Long.class),planId));
              }else {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  if(!StringUtils.isEmpty(planTypeId)){
            		  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
                	  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                	  lstPredicates.add(p1);
                  }
            	  } 
              }
              
              //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              
              
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
              
              if( !PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
		              Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
		              if(itemHelper.getCurrentUserItems().size() > 0 ){
		  					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
		  		            lstPredicates.add(p3);
		                  }else{
		  					Predicate p3=criteriaBuilder.and(item.isNull());
		  		            lstPredicates.add(p3);
		                  }
            	  }
              }else {
            	  if(!StringUtils.isEmpty(functionOffice)){
            		  Join<Object, Object> fo = plan.join("functionOffice",JoinType.INNER);
            	  Predicate p1=criteriaBuilder.equal(fo.get("id").as(Long.class), functionOffice);
            	  lstPredicates.add(p1);
            	  }
            	  
              }
              if(!StringUtils.isEmpty(month)){
            		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThanOrEqualTo(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
              }else {
            	  if(!StringUtils.isEmpty(year)){
            		  DateTime dateTime = new DateTime(year);
  					// 获取传入月份第一天然后减1就是上月最后一天
  					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
  					// 结束时间大于上月最后一天
  					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThanOrEqualTo(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
            	  }
              }
              
              lstPredicates.add(criteriaBuilder.isNotNull(root.get("completeDate").as(Date.class)));
              
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		List<PlanNode> findAll = planNodeRepository.findAll(spec);
		
		return findAll;
	}


	@Override
	public Pagination<PlanNode> findSupervise(Long year, Long planTypeId,String code,String nodeName, Long itemId, Long month, int pageIndex, int pageSize) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              
              Join<Object, Object> plan = root.join("plan",JoinType.INNER);
              lstPredicates.add(criteriaBuilder.equal(plan.get("state").as(Integer.class),2));
              
              if(!StringUtils.isEmpty(planTypeId)){
            	  Join<Object, Object> planType = plan.join("planType",JoinType.INNER);
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
                  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planTypeId);
                  lstPredicates.add(p1);
            	  }
              }
              
              
              //月度只统计专项和职能 
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  Join<Object, Object> planType = plan.join("planType", JoinType.INNER);
            	  In<Object> in = criteriaBuilder.in(planType.get("code"));
            	  in.value(PlanType.FUNC_TYPE.getCode());
            	  in.value(PlanType.PROFESSION_TYPE.getCode());
            	  lstPredicates.add(in);
              }
              
              
              if(!StringUtils.isEmpty(nodeName)){
            	  Predicate p1=criteriaBuilder.like(root.get("nodeName").as(String.class),"%"+nodeName+"%");
            	  lstPredicates.add(p1);
              }
              
              if(!StringUtils.isEmpty(itemId)){
            	  Join<Object, Object> item = plan.join("item",JoinType.INNER);
                  Predicate p1=criteriaBuilder.equal(item.get("id").as(Long.class), itemId);
                  lstPredicates.add(p1);
              }
              
              
              //判断 职能权限
              if(PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  
                    List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                    Predicate pfunction = null;
                    if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                    	 pfunction=plan.get("functionOffice").in(functionOffices);
                    }else{
                    	  pfunction=plan.get("functionOffice").isNull();
                    }
      		       lstPredicates.add(criteriaBuilder.and(pfunction)); 
              }
              
             
              
              //判断项目权限和职能权限
              if(PlanType.MONTH_TYPE.getCode().equals(code)) {
            	  
                  List<Item> items = itemHelper.getCurrentUserItems();
                  List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
                  Predicate pitem = null;
                  Predicate pfunction = null;
                  if(itemHelper.getCurrentUserItems().size() > 0 ){
                  	pitem=plan.get("item").in(items);
                  }else{
                  	pitem=plan.get("item").isNull();
                  }
                  if(itemHelper.getCurrentUserFunctionOffices().size() > 0 ){
                  	 pfunction=plan.get("functionOffice").in(functionOffices);
                  }else{
                  	  pfunction=plan.get("functionOffice").isNull();
                  }
  		       lstPredicates.add(criteriaBuilder.or(pitem, pfunction));
              }
              
              
              if( !PlanType.FUNC_TYPE.getCode().equals(code)) {
            	  if(!PlanType.MONTH_TYPE.getCode().equals(code)) {
	              Join<Object, Object> item = root.join("plan",JoinType.INNER).join("item",JoinType.INNER);
	              if(itemHelper.getCurrentUserItems().size() > 0 ){
	  					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
	  		            lstPredicates.add(p3);
	                  }else{
	  					Predicate p3=criteriaBuilder.and(item.isNull());
	  		            lstPredicates.add(p3);
	                  }
            	  }
              } 
              //状态为风险,失控
        	  In<Object> in = criteriaBuilder.in(root.get("riskState"));
        	  in.value(1);
        	  in.value(2);
        	  lstPredicates.add(in);
          
              if(!StringUtils.isEmpty(month)){
          		DateTime dateTime = new DateTime(month);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThanOrEqualTo(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
            }else {
          	  if(!StringUtils.isEmpty(year)){
          		  DateTime dateTime = new DateTime(year);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfYear().withMinimumValue().toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfYear().withMaximumValue().toDate();
					//结束日期>当前月份的最小天
					Predicate greaterThan = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class),mini);
					//开始时间<当前月份的最大一天
					Predicate lessThan = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate").as(Date.class),max);
					//延期后的日期
					Predicate greaterThan1 = criteriaBuilder.greaterThanOrEqualTo(root.get("delayDate").as(Date.class),mini);
					Predicate and = criteriaBuilder.and(greaterThan,lessThan);
					Predicate and1 = criteriaBuilder.and(greaterThan1,lessThan);
					lstPredicates.add(criteriaBuilder.or(and, and1));
          	  }
            }
            
              
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<PlanNode> pageT =  planNodeRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<PlanNode> pagination = new Pagination<PlanNode>();
		pagination.setTotal(planNodeRepository.count(spec));
		List<PlanNode> plans = pageT.getContent();
	 
		pagination.setData(plans);
		return pagination;
	}





 


}